//
//  SLAccessDurtionVC.m
//  SlockApp
//
//  Created by 凯朱 on 2024/7/26.
//

#import "SLAccessDurtionVC.h"
#import "SCSxTimeRangePickView.h"
#import "SLDaySelectAlertView.h"
#import "SLFingerCreateVC.h"
#import "SLSetAccessNameVC.h"
@interface SLAccessDurtionVC ()<DatePickerViewDelegate>{
    UIView *_dateBgView;
    NSString *_formart;
    NSString *_submitFormart;
    
    
}
@property (nonatomic, strong)NSArray *selectDays;
@property (nonatomic, strong)SCSxTimeRangePickView *timeRangePickView;
@property (nonatomic, strong)SLDaySelectAlertView *daySelectView;
@end

@implementation SLAccessDurtionVC
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpSubViews];
}

-(void)setUpSubViews{
    
    self.title = kLocalString(@"Make new access");
    _topLbl.text = kLocalString(@"Set a duration time");
    _formart = @"MMM. d, yyyy";
    _selectDays = @[kLocalString(@"Monday"),kLocalString(@"Tuesday"),kLocalString(@"Wednesday"),kLocalString(@"Thursday"),kLocalString(@"Friday"),kLocalString(@"Saturday"),kLocalString(@"Sunday")];
    
    _bgView.layer.cornerRadius = 20;
    _bgView.layer.masksToBounds = YES;
    _sureBtn.btnStyle = 1;
    [_sureBtn setTitle:kLocalString(@"Next") forState:UIControlStateNormal];
    
    
    _listContentLbl1.text = [[NSDate date] stringWithFormat:_formart];
    _listContentLbl2.text = [[[NSDate date] dateByAddingDays:1] stringWithFormat:_formart];
    _listTimeLbl1.text = @"08:00";
    _listTimeLbl2.text = @"18:00";
    
    _listLbl1.text = kLocalString(@"From");
    _listLbl2.text = kLocalString(@"To");
    _listLbl3.text = kLocalString(@"Time repeat");
    _listLbl4.text = kLocalString(@"Every");
    
    _listContentLbl1.layer.cornerRadius = 10;
    _listContentLbl1.layer.masksToBounds = YES;
    _listTimeLbl1.layer.cornerRadius = 10;
    _listTimeLbl1.layer.masksToBounds = YES;
    
    _listContentLbl2.layer.cornerRadius = 10;
    _listContentLbl2.layer.masksToBounds = YES;
    _listTimeLbl2.layer.cornerRadius = 10;
    _listTimeLbl2.layer.masksToBounds = YES;
    
    kWeakSelf(self);
    _listContentLbl1.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        [weakself handleListAction:1];
    }];
    [_listContentLbl1 addGestureRecognizer:tap1];
    
    _listTimeLbl1.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        [weakself handleListAction:2];
    }];
    [_listTimeLbl1 addGestureRecognizer:tap2];
    
    _listContentLbl2.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap3 = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        [weakself handleListAction:3];
    }];
    [_listContentLbl2 addGestureRecognizer:tap3];
    
    _listTimeLbl2.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap4 = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        [weakself handleListAction:4];
    }];
    [_listTimeLbl2 addGestureRecognizer:tap4];
    
    _listView4.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap5 = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        [weakself handleListAction:5];
    }];
    [_listView4 addGestureRecognizer:tap5];
    
    [self updateListView];
    
}

-(void)updateListView{
    if (_changeBtn.isOn) {
        _listView4.hidden = NO;
        _line3.hidden = NO;
        _bgViewHeight.constant = 200;
    }else{
        _line3.hidden = YES;
        _listView4.hidden = YES;
        _bgViewHeight.constant = 150;
    }
}

-(void)handleListAction:(NSInteger)index{
    if (index == 1) {//选择开始日期
        [self showDateViewWith:1];
    }else if (index == 2) {//选择开始时间
        [self showDateViewWith:2];
    }else if (index == 3){//选择结束日期
        [self showDateViewWith:3];
    }else if (index == 4){//选择结束时间
        [self showDateViewWith:4];
    }else if (index == 5){//选择重复
        [self showDaySelectView];
    }
}

- (IBAction)changeAction:(UISwitch *)sender {
    [self updateListView];
}

-(void)showDateViewWith:(NSInteger)tag{
    CGSize size = [[UIScreen mainScreen] bounds].size;
    _dateBgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
    _dateBgView.backgroundColor = [kHexColor(@"#0C0C0C") colorWithAlphaComponent:0.8];
    [[[UIApplication sharedApplication] keyWindow] addSubview:_dateBgView];
    DatePickerView *datePickerView = [[DatePickerView alloc] initWithFrame:CGRectMake(0, KScreenHeight - 250, KScreenWidth, 250)];
    datePickerView.layer.cornerRadius = 10;
    datePickerView.layer.masksToBounds = YES;
    datePickerView.fundelegate = self;
    datePickerView.datePickerMode = UIDatePickerModeDate;
    if (tag == 2 || tag == 4) {
        datePickerView.datePickerMode = UIDatePickerModeTime;
    }
    datePickerView.tag = tag;
    NSDate *now = [NSDate date];
    [datePickerView.datePicker setDate:now animated:YES];
    datePickerView.datePicker.frame = CGRectMake(0, 50, KScreenWidth, 200);
    [[[UIApplication sharedApplication] keyWindow] addSubview:datePickerView];
}

-(void)didDatePickerViewDoneClicked:(DatePickerView *)aPickerView{
    NSInteger tag = aPickerView.tag;
    NSDate *selectDate = [aPickerView selectDate];
    if (tag == 1) {
        _listContentLbl1.text = [selectDate stringWithFormat:_formart];;
    }else if(tag == 2){
        _listTimeLbl1.text = [selectDate stringWithFormat:@"HH:mm"];
    }else if(tag == 3){
        _listContentLbl2.text = [selectDate stringWithFormat:_formart];
    }else if(tag == 4){
        _listTimeLbl2.text = [selectDate stringWithFormat:@"HH:mm"];
    }
    [_dateBgView removeFromSuperview];
    [aPickerView removeFromSuperview];
}

- (void)didDatePickerViewCancelClicked:(DatePickerView*)aPickerView{
    [_dateBgView removeFromSuperview];
    [aPickerView removeFromSuperview];
}

-(void)showDaySelectView{
    if (_daySelectView) {
        return;
    }
    _daySelectView = [SLDaySelectAlertView viewFromXIB];
    [self.view addSubview:_daySelectView];
    
    [_daySelectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
    }];
    _daySelectView.selects = (NSMutableArray *)_selectDays;
    kWeakSelf(self);
    [_daySelectView setBtnBlock:^(NSArray * _Nonnull selects,NSInteger index) {
        [weakself.daySelectView removeFromSuperview];
        weakself.daySelectView = nil;
        if (index == 1) {
            weakself.selectDays = selects;
            weakself.listContentLbl4.text = [weakself dayStr];
        }
    }];
}

-(NSArray *)getWeekDaysValue{
    if (_selectDays == nil || _selectDays.count == 0) {
        return @[@"1",@"2",@"3",@"4",@"5",@"6",@"0"];
    }
    NSMutableArray *arr = [NSMutableArray new];
    [_selectDays enumerateObjectsUsingBlock:^(NSString *dayStr, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([dayStr isEqualToString:kLocalString(@"Monday")]) {
            [arr addObject:@"1"];
        }else if ([dayStr isEqualToString:kLocalString(@"Tuesday")]){
            [arr addObject:@"2"];
        }else if ([dayStr isEqualToString:kLocalString(@"Wednesday")]){
            [arr addObject:@"3"];
        }else if ([dayStr isEqualToString:kLocalString(@"Thursday")]){
            [arr addObject:@"4"];
        }else if ([dayStr isEqualToString:kLocalString(@"Friday")]){
            [arr addObject:@"5"];
        }else if ([dayStr isEqualToString:kLocalString(@"Saturday")]){
            [arr addObject:@"6"];
        }else if ([dayStr isEqualToString:kLocalString(@"Sunday")]){
            [arr addObject:@"0"];
        }
    }];
    return arr;
}


-(NSString *)dayStr{
    if (_selectDays.count == 7) {
        return kLocalString(@"Every day");
    }else{
        NSString *dayStr = [_selectDays componentsJoinedByString:@"./"];
        return dayStr;
    }
}


- (IBAction)sureAction:(id)sender {
    
    //更新提交的值
    NSString *startDateStr = [self submitDateStrWith:_listContentLbl1.text];
    NSString *endDateStr = [self submitDateStrWith:_listContentLbl2.text];
    if (_changeBtn.isOn) {//有循环
        _keyModel.weekDays = [self getWeekDaysValue];
    }else{//没循环
        _keyModel.weekDays = @[];
    }
    _keyModel.startTime = [NSString stringWithFormat:@"%@ %@:00",startDateStr,_listTimeLbl1.text];
    _keyModel.endTime = [NSString stringWithFormat:@"%@ %@:00",endDateStr,_listTimeLbl2.text];
    _keyModel.dayStart = _listTimeLbl1.text;
    _keyModel.dayEnd = _listTimeLbl2.text;
    
    if (_keyModel.lockType == 1) {
        SLFingerCreateVC *vc = [[SLFingerCreateVC alloc] init];
        vc.keyModel = _keyModel;
        [self.navigationController pushViewController:vc animated:YES];
    }else{
        [ZCTool uploadBlueToothKeyData:_keyModel];
    }
}

-(NSString *)submitDateStrWith:(NSString *)dateStr{
    NSDate *date = [NSDate dateFromDateStr:dateStr formatStr:_formart];
    NSString *sumbitDateStr = [NSString dateStrFromDate:date formatStr:@"yyyy-MM-dd"];
    return sumbitDateStr;
}

@end

